#!/usr/bin/env python
from Const import SCREENWIDTH, SCREENHEIGHT

class Camera:
    object = None
    offset = (0, 0)
    def __init__(self, screen, level):
        self.screen = screen
        self.level = level
    
    def get_x(self):
        return self.get_center_x() - SCREENWIDTH / 2
    
    def get_y(self):
        return self.get_center_y() - SCREENHEIGHT / 2
    
    def get_center_x(self):
        xc = self.object.rect[0] + self.object.rect[2] / 2
        if xc - SCREENWIDTH / 2 < 0:
            xc = SCREENWIDTH / 2
            return xc
        if xc + SCREENWIDTH / 2 > self.level.level_width:
            xc = self.level.level_width - SCREENWIDTH / 2
            return xc
        return xc
    
    def get_center_y(self):
        yc = self.object.rect[1] + self.object.rect[3] / 2
        if yc - SCREENHEIGHT / 2 < 0:
            yc = SCREENHEIGHT / 2
            return yc
        if yc + SCREENHEIGHT / 2 > self.level.level_height:
            yc = self.level.level_height - SCREENHEIGHT / 2
            return yc
        return yc
    
    def blit(self, img, pos, blend = 0):
        x = pos[0]
        y = pos[1]
        if self.object != None:
            xc = self.get_center_x()
            yc = self.get_center_y()
            x -= xc
            y -= yc
            x += SCREENWIDTH / 2
            y += SCREENHEIGHT / 2
        if x + img.get_width() >= 0 and y + img.get_height() >= 0:
            if x < SCREENWIDTH and y < SCREENHEIGHT:
                self.screen.blit(img, (x, y), None, blend)
            
            